﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;

using FoxConn.Lib.Dbo;
using FoxConn.Lib.Winform;
using OracleToInformix.Source;

namespace OracleToInformix
{
    partial class HrService : ServiceBase
    {
        public HrService()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            // TODO: 在此处添加代码以启动服务。
            DBException dbex = new DBException();

            try
            {
                int intTimeInterval = 1000;
                XMLutil xmlOperator = new XMLutil();

                string strTimeInterval = xmlOperator.GetXMLParameter("TimerInterval");

                if (!strTimeInterval.Equals(""))
                {
                    intTimeInterval = Convert.ToInt32(strTimeInterval);
                }

                this.timerToInformix.Enabled = true;
                this.timerToInformix.Interval = intTimeInterval;
                this.timerToInformix.Start();

                dbex.SaveErrorLog("timerToOracle Start.Interval:" + this.timerToInformix.Interval.ToString());
            }
            catch (Exception ex)
            {
                dbex.SaveException(ex, "Service OnStart Error.");
            }
            finally
            { }
        }

        protected override void OnStop()
        {
            // TODO: 在此处添加代码以执行停止服务所需的关闭操作。
            this.timerToInformix.Enabled = false;
            this.timerToInformix.Stop();

            DBException dbex = new DBException();
            dbex.SaveErrorLog("timerToInformix Stop");
        }

        private void timerToInformix_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            DBException dbex = new DBException();

            try
            {
                XMLutil xmlOperator = new XMLutil();
                string strStartHour = xmlOperator.GetXMLParameter("StartHour");
                string strStartMinute = xmlOperator.GetXMLParameter("StartMinute");

                int intStartHour = 2;
                int intStartMinute = 0;

                if (strStartHour != null && !strStartHour.Equals(""))
                {
                    intStartHour = Convert.ToInt32(strStartHour);
                }

                if (strStartMinute != null && !strStartMinute.Equals(""))
                {
                    intStartMinute = Convert.ToInt32(strStartMinute);
                }

                if (DateTime.Now.Hour == intStartHour && DateTime.Now.Minute == intStartMinute && DateTime.Now.Second == 0)
                {
                    dbex.SaveErrorLog("Starting OracleToInfomix Function.");
                    MainFunction mf = new MainFunction();
                    mf.OracleToInformix();
                }
            }
            catch (Exception ex)
            {
                dbex.SaveException(ex, "Start Service Error.");
            }
            finally
            {
            }
        }
    }
}
